package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.CarouselBanner;
import com.zhentao.pojo.SysUser;
import com.zhentao.pojo.SystemLog;
import com.zhentao.service.CarouselBannerService;
import com.zhentao.service.SystemLogService;
import com.zhentao.service.SysUserService;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system")
public class SystemController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SystemLogService logService;

    @Autowired
    private CarouselBannerService bannerService;

    // 用户管理
    @RequestMapping("/user/list")
    public Result getUserList(@RequestBody com.zhentao.pojo.Page page) {
        Page<SysUser> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        Page<SysUser> result = userService.page(page1,queryWrapper);
        return Result.OK(result);
    }

    @RequestMapping("/user/add")
    public Result addUser(@RequestBody SysUser user) {
        userService.save(user);
        return Result.OK();
    }

    @RequestMapping("/user/update")
    public Result updateUser(@RequestBody SysUser user) {
        userService.updateById(user);
        return Result.OK();
    }

    @RequestMapping("/user/delete/{id}")
    public Result deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return Result.OK();
    }

    // 系统日志
    @RequestMapping("/log/list")
    public Result getLogList(@RequestBody com.zhentao.pojo.Page page) {
        Page<SystemLog> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<SystemLog> queryWrapper = new QueryWrapper<>();
        Page<SystemLog> result = logService.page(page1,queryWrapper);
        return Result.OK(result);
    }

    // 轮播图管理
    @RequestMapping("/banner/list")
    public Result getBannerList() {
        List<CarouselBanner> list = bannerService.list();
        return Result.OK(list);
    }

    @RequestMapping("/banner/add")
    public Result addBanner(@RequestBody CarouselBanner banner) {
        bannerService.save(banner);
        return Result.OK();
    }

    @RequestMapping("/banner/update")
    public Result updateBanner(@RequestBody CarouselBanner banner) {
        bannerService.updateById(banner);
        return Result.OK();
    }

    @RequestMapping("/banner/delete/{id}")
    public Result deleteBanner(@PathVariable Long id) {
        bannerService.removeById(id);
        return Result.OK();
    }
}
